package com.zd.springcloud.tx.util;

import com.mysql.cj.jdbc.Driver;
import com.zd.springcloud.tx.mapper.AdminMapper;
import com.zd.springcloud.tx.mapper.UserMapper;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;



import javax.sql.DataSource;

public class FactoryUtil {


    //返回aaa数据库的SqlSessionFactory
    public static SqlSessionFactory getAAASqlSessionFactory(){
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://47.101.218.65:3306/aaa?serverTimezone=UTC&useSSL=false";
        String username = "root";
        String password = "root";

        DataSource dataSource = new PooledDataSource(driverClassName,url,username,password);

        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);

        Configuration configuration = new Configuration(environment);
        configuration.addMapper(AdminMapper.class);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        return sqlSessionFactory;
    }

    //返回bbb数据库的SqlSessionFactory
    public static SqlSessionFactory getBBBSqlSessionFactory(){
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://47.101.218.65:3307/aaa?serverTimezone=UTC&useSSL=false";
        String username = "root";
        String password = "root";

        DataSource dataSource = new PooledDataSource(driverClassName,url,username,password);


        TransactionFactory transactionFactory = new JdbcTransactionFactory();


        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(UserMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        return sqlSessionFactory;
    }

}
